//
//  ZZProgressHUDShowView.m
//  ZZProgressHUD
//
//  Created by czz on 2018/5/23.
//  Copyright © 2018年 czz. All rights reserved.
//

#import "ZZProgressHUDShowView.h"

@implementation ZZProgressHUDShowView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(UIActivityIndicatorView *)activityIndicatorView{
    if (!_isLoading) return nil;//如果不是加载状态不绘制加载框
    if (!_activityIndicatorView) {
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        if (self.isLoading) {
            [_activityIndicatorView startAnimating];
        }
        [self.contentView addSubview:_activityIndicatorView];
    }
    return _activityIndicatorView;
}
-(UIVisualEffectView *)effectBgView{
    if (!_effectBgView) {
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        _effectBgView = [[UIVisualEffectView alloc] initWithEffect:effect];
        [self insertSubview:_effectBgView belowSubview:self.contentView];
    }
    return _effectBgView;
}
-(void)setInfoImageSize:(CGSize)infoImageSize{
    _infoImageSize = infoImageSize;
    [self setImageSize:_infoImageSize];
}
-(void)setIsLoading:(BOOL)isLoading{
    _isLoading = isLoading;
    if (_isLoading) {
        self.imageView.hidden = YES;
        _activityIndicatorView.hidden = NO;
        [_activityIndicatorView startAnimating];
    }else{
        self.imageView.hidden = NO;
        [self.activityIndicatorView stopAnimating];
        _activityIndicatorView.hidden = YES;
        [self setImageSize:_infoImageSize];
        [_activityIndicatorView stopAnimating];
    }
    [self updateUIFrame];
}
-(void)setMinSize:(CGSize)minSize{
    _minSize = minSize;
    [self updateUIFrame];
}
-(void)setMaxSize:(CGSize)maxSize{
    _maxSize = maxSize;
    self.titleLabelMaxSizeWhenChanged = _maxSize;
    [self updateUIFrame];
}
-(void)setContentToBorderSpace:(CGFloat)contentToBorderSpace{
    _contentToBorderSpace = contentToBorderSpace;
    [self updateUIFrame];
}
-(void)updateUIFrame{
    [super updateUIFrame];
    if (self.isLoading){
        if (isChangeImageSizeWithActivityShow){
            isChangeImageSizeWithActivityShow = NO;
            self.activityIndicatorView.center = self.imageView.center;
            return;
        }
        isChangeImageSizeWithActivityShow = YES;
        [self setImageSize:self.activityIndicatorView.frame.size];
    }
    __weak typeof (self) weakSelf = self;
    if (!CGSizeEqualToSize(weakSelf.minSize, CGSizeZero)) {
        CGRect frame = self.contentView.frame;
        if (frame.size.width < (weakSelf.minSize.width - (weakSelf.contentToBorderSpace * 2)) ) {
            frame.size.width = weakSelf.minSize.width;
        }else{
            frame.size.width += (weakSelf.contentToBorderSpace * 2);
        }
        if (frame.size.height < (weakSelf.minSize.height - (weakSelf.contentToBorderSpace * 2))) {
            frame.size.height = weakSelf.minSize.height;
        }else{
            frame.size.height += (weakSelf.contentToBorderSpace * 2);
        }
        frame.origin = self.frame.origin;
        CGRect contentFrame = self.contentView.frame;
        contentFrame.origin = CGPointMake((frame.size.width - self.contentView.frame.size.width)/2., (frame.size.height - self.contentView.frame.size.height)/2.);
        
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = frame;
            self.effectBgView.frame = self.bounds;
            self.contentView.frame = contentFrame;
        }];
    }
}
@end
